package com.nf.health.app.activity.home;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.home.adapter.CharacteristicDepartmentAdapter;
import com.nf.health.app.activity.home.bean.CharacteristicBean;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 特色科室  Ian
 * <p>
 * Created by Administrator on 2017/4/7.
 */
public class CharacteristicDepartmentActivity extends BaseActivity {
    public static  String HID = "com.nf.hid";
    List<CharacteristicBean> list;
    List<CharacteristicBean> listAll = new ArrayList<CharacteristicBean>();

    int pageNo = 1,pageSize = 20;

    CharacteristicDepartmentAdapter charadapter;

    @Bind(R.id.titlebar_title_tv)//title
            TextView titlebarTitleTv;
    @Bind(R.id.characteristic_dragListview)
    DragListView characteristicDragListview;//特殊科室 listview
    private String hid = "";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.layout_activity_characteristic_department);
        ButterKnife.bind(this);
        initView();
        initData("refresh");
    }

    private void initView() {
        hid = getIntent().getStringExtra(HID);
        titlebarTitleTv.setText("特色科室");

        characteristicDragListview.setRefreshableAndLoadMoreable(true, false);
        characteristicDragListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                pageNo = 1;
                initData("refresh");
            }

            @Override
            public void onLoadMore() {
                pageNo++;
                initData("more");
            }
        });

        characteristicDragListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Bundle bundle = new Bundle();
                bundle.putString("url", list.get(position - 1).getTourl());
                bundle.putString("name", list.get(position - 1).getName());
                ActivityUtils.showActivity(CharacteristicDepartmentActivity.this, CharacteristicDepartmentResultActivity.class, bundle);

            }
        });

    }

    private void initData(String flag) {
        provider.requestCharacteristicDepartment(pageNo+"",pageSize+"",hid,flag);
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if("refresh".equals(actionName)){
            characteristicDragListview.completeRefresh();
            list = (List<CharacteristicBean>) object;
            listAll.clear();
            listAll.addAll(list);
            charadapter = new CharacteristicDepartmentAdapter(this,listAll);
            characteristicDragListview.setAdapter(charadapter);
            if (list.size() < pageSize) {
                characteristicDragListview.setRefreshableAndLoadMoreable(true, false);
            } else {
                characteristicDragListview.setRefreshableAndLoadMoreable(true, true);
            }
        }else if("more".equals(actionName)){
            characteristicDragListview.completeLoadMore();
            list.clear();
            list = (List<CharacteristicBean>) object;

            if (list.size()>0) {
                listAll.addAll(list);
                charadapter.notifyDataSetChanged();
            } else {
                showToast("没有更多数据~");
            }
            if (list.size() < pageSize) {
                characteristicDragListview.setRefreshableAndLoadMoreable(true, false);
            } else {
                characteristicDragListview.setRefreshableAndLoadMoreable(true, true);
            }

        }
    }
}
